/*
 * LayoutAdditive.java
 *
 * Copyright (c) 2007 Aris Bartee
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * Created on July 9, 2007, 11:13 PM
 */
package afterburner.jsp.fuel;

import java.lang.reflect.Method;
import java.text.MessageFormat;

import javax.servlet.ServletContext;

import afterburner.jsp.Base;
import afterburner.jsp.JSP;
import afterburner.jsp.annotations.DefaultAction;

/**
 *
 * @author arisbartee
 */
public class ActionAdditive implements FuelAdditive {

    private String twoDefaultActionMessage = "{0} and {1} are both defined as the default action.";

    public void contextInitialized(ServletContext context) {
    }

    public void contextDestroyed(ServletContext context) {
    }

    public void preJSP(Base baseJsp) {
        JSP jsp = (JSP) baseJsp;
        boolean found = false;
        String exe = jsp.getRequest().getParameter("exe");
        if (exe != null) {
            jsp.setAction(exe);
        } else {
            String path = jsp.getRequest().getRequestURI();
            int slash = path.lastIndexOf("/");
            int dotJsp = path.lastIndexOf(".jsp");
            if (dotJsp == -1) {
                dotJsp = path.length();
            }
            jsp.setAction(path.substring(slash + 1, dotJsp));
            if (jsp.getAction() == "") {
                jsp.setAction(null);
            }
            try {
                if (jsp.getAction() != null) {
                    jsp.getClass().getMethod(jsp.getAction(), new Class[0]);
                }
            } catch (NoSuchMethodException nsme) {
                jsp.setAction(null);
            }
        }
        if (jsp.getAction() == null) {
            try {
                Method methods[] = jsp.getClass().getMethods();
                for (Method method : methods) {
                    DefaultAction da = method.getAnnotation(DefaultAction.class);
                    if (da != null) {
                        if (!found) {
                            found = true;
                            jsp.setAction(method.getName());
                        } else {
                            throw new RuntimeException(MessageFormat.format(twoDefaultActionMessage, jsp.getAction(), method.getName()));
                        }
                    }
                }
            } catch (Exception e) {
                baseJsp.getServletConfig().getServletContext().log(e.getMessage());
            }
        }

    }

    public void postJSP(Base baseJsp) {
        JSP jsp = (JSP) baseJsp;
        jsp.setAction(null);
    }
}
